package com.tgy.programmer_code_interview_guide._01_stack_queue;

import java.util.Stack;

/**
 * @Author: tgy
 * @Date: 1/14/21 3:14 PM
 *
 *  https://leetcode-cn.com/problems/implement-queue-using-stacks/
 */
public class _2_使用栈实现队列 {

    private Stack<Integer> firstStack = new Stack<>();
    private Stack<Integer> secondStack = new Stack<>();


    /** Push element x to the back of queue. */
    public void push(int x) {

        firstStack.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {

        if (secondStack.isEmpty()) {

            while (!firstStack.isEmpty()) {

                secondStack.push(firstStack.pop());
            }
        }


        return secondStack.pop();
    }

    /** Get the front element. */
    public int peek() {

        if (secondStack.isEmpty()) {

            while (!firstStack.isEmpty()) {

                secondStack.push(firstStack.pop());
            }
        }

        return secondStack.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {

        return firstStack.isEmpty() && secondStack.isEmpty();
    }

  public static void main(String[] args) {
    //

      _2_使用栈实现队列 queue = new _2_使用栈实现队列();

      queue.push(1);
      queue.push(2);
      queue.push(3);
      queue.push(4);

      System.out.println(queue.pop());
      queue.push(5);
//    System.out.println(queue.pop());
      while (!queue.empty()) {

        System.out.println(queue.pop());
      }
  }

}
